Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Is there any way that I can use variables while changing the the properties of document.getElemtentbyId(").style in JavaScript?

Is there any way that I can change the properties in document.getElementbyId.style with a variable that I received from the Parameters of the function?

function toggleFunction(elementId, property1, property2) {
    let element = document.getElementById(elementId)
    if (element.style.display == property1) {
        element.style.display = property2
    } else {
        element.style.display = property1
    }
}

The above code is not giving me the desired result. Is there a way that I can use variable values that I got from parameters for writing Properties?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe I didn't catch what you need but its simple. Your code should work good if you use correct arguments for you func. But don't use "element" name twice. That is bad practise

const elementId = 'your-element-id'
const property1 = 'none'
const property2 = 'block'

const element = document.getElementById(elementId)
element.style.display = property1 // Set element's display = 'none'

The same way for another style properties:

element.style.width = '50%'
element.style.color = '#ff0000'
element.style.marginTop = '14px'

Read more -> HTML DOM Style Object

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda